package com.example.rule.config;

import com.example.rule.db.entity.PymtRule;
import com.example.rule.db.entity.PymtRuleOperator;
import com.example.rule.db.entity.PymtRuleType;
import com.example.rule.db.repository.PymtRuleOperatorRepository;
import com.example.rule.db.repository.PymtRuleRepository;
import com.example.rule.db.repository.PymtRuleTypeRepository;
import com.example.rule.util.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jason - Chusifeng
 * @version 1.0.0
 * @className ServerStartConfig.java
 * @packageName com.example.controller
 * @createTime 2022年10月10日 10:27:00
 * @description
 */
@Component
@Slf4j
public class ServerCacheConfig implements CommandLineRunner {
    @Autowired
    private PymtRuleRepository ruleRepository;
    @Autowired
    private PymtRuleTypeRepository typeRepository;
    @Autowired
    private PymtRuleOperatorRepository operatorRepository;

    @Override
    public void run(String... args) throws Exception {
        log.info("start init fee rule to cache");
        List<PymtRule> ruleList = ruleRepository.findAll();
        List<PymtRuleType> typeList = typeRepository.findAll();
        List<PymtRuleOperator> operatorList = operatorRepository.findAll();
        CacheUtil.init();
        CacheUtil.cacheType(typeList);
        CacheUtil.cacheOperator(operatorList);
        CacheUtil.cacheRule(ruleList);
        log.info("finish cache fee rule");
    }
}
